package com.course.cdspringboot.util;

import java.util.HashMap;
import java.util.Map;

public class Result {
    private int code;
    private String message;
    private Map<String, Object> data = new HashMap<String, Object>();

    private Result() {
    }

    public static Result login_ok() {
        Result result = new Result();
        result.setCode(ResultCode.LOGIN_SUCCESS);
        result.setMessage("登录成功");
        return result;
    }

    public static Result login_error() {
        Result result = new Result();
        result.setCode(ResultCode.LOGIN_ERROR);
        result.setMessage("登录失败");
        return result;
    }



    public static Result update_ok() {
        Result result = new Result();
        result.setCode(ResultCode.UPDATE_SUCCESS);
        result.setMessage("更新成功，有条目的值得到更新");
        return result;
    }
    public static Result update_error() {
        Result result = new Result();
        result.setCode(ResultCode.UPDATE_ERROR);
        result.setMessage("更新失败，没有任何条目的值得到更新");
        return result;
    }

    public static Result delete_ok() {
        Result result = new Result();
        result.setCode(ResultCode.DELETE_SUCCESS);
        result.setMessage("删除成果");
        return result;
    }
    public static Result delete_error() {
        Result result = new Result();
        result.setCode(ResultCode.DELETE_ERROR);
        result.setMessage("删除失败");
        return result;
    }



    public static Result insert_ok() {
        Result result = new Result();
        result.setCode(ResultCode.ADD_SUCCESS);
        result.setMessage("添加成功");
        return result;
    }

    public static Result insert_error() {
        Result result = new Result();
        result.setCode(ResultCode.ADD_ERROR);
        result.setMessage("添加失败");
        return result;
    }


    public static Result insert(int error_code, String error_msg) {
        if (error_code == 0) {
            return Result.insert_ok();
        } else if(error_code != -1) {
            return Result.insert_error().data("error_msg", error_msg);
        }

        return Result.insert_error().data("error_msg", "未知错误");
    }


    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    //    public static void main(String[] args) {
//        Result result = Result.ok().data("token", "12356");
//        System.out.println(result.getCode());
//        System.out.println(result.getData());
//    }
}
